Skip to content

Code quality improvements: robust error handling, HA best practices, stale translation cleanup#5

Merged
gensyn merged 3 commits intomainfrom
copilot/review-code-quality-and-best-practices
Mar 17, 2026
Merged

Code quality improvements: robust error handling, HA best practices, stale translation cleanup#5
gensyn merged 3 commits intomainfrom
copilot/review-code-quality-and-best-practices

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 16, 2026

Complete code review pass addressing fragile error detection, HA anti-patterns, dead code, and stale/missing translation keys.

coordinator.py

  • Replace fragile strerror string match with isinstance(exc, socket.gaierror) — the original check only caught one locale-specific DNS error message and missed others (e.g. "Name or service not known")
  • Add _LOGGER.warning() in all exception handlers — _LOGGER was defined but never used; log messages now include host/username context for easier diagnosis
  • Fix misleading default known-hosts pathPath('~', '.ssh', CONF_KNOWN_HOSTS) coincidentally worked (the constant value equals the filename) but was semantically wrong; replaced with the literal "known_hosts"
# Before — locale-sensitive, misses other gaierror variants
except OSError as e:
    if e.strerror == 'Temporary failure in name resolution':
        raise ServiceValidationError(...)

# After — catches all address-resolution failures
except OSError as exc:
    if isinstance(exc, socket.gaierror):
        _LOGGER.warning("Host %s is not reachable: %s", host, exc)
        raise ServiceValidationError(...)

config_flow.py

  • Add single_instance_allowed = True — the HA-native class attribute for single-instance integrations; allows the framework to abort duplicate flows before async_step_user is reached (HA ≥ 2024.x)
  • Remove hass.data.get(DOMAIN) redundant check — non-standard secondary guard that required # pylint: disable=no-member; _async_current_entries() is the correct HA pattern
  • Remove unused logging / _LOGGER

strings.json / translations/en.json / translations/de.json

  • Remove stale script_file service field and script_file_not_found exception (feature was replaced by input)
  • Add missing password and input service fields
  • Fix command_or_input message: "script file" → "input"
  • Add missing integration_not_set_up exception (used in __init__.py but absent from all translation files)

📍 Connect Copilot coding agent with Jira, Azure Boards or Linear to delegate work to Copilot in one click without leaving your project management tool.

…le_instance_allowed, strings cleanup

Co-authored-by: gensyn <36128035+gensyn@users.noreply.github.com>
Copilot AI changed the title [WIP] Conduct complete code review for Python and Home Assistant best practices Code quality improvements: robust error handling, HA best practices, stale translation cleanup Mar 16, 2026
Copilot AI requested a review from gensyn March 16, 2026 21:03
@gensyn gensyn marked this pull request as ready for review March 17, 2026 10:27
@gensyn gensyn merged commit 2c86e76 into main Mar 17, 2026
4 checks passed
@gensyn gensyn deleted the copilot/review-code-quality-and-best-practices branch March 17, 2026 10:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants